package {
	import com.combiform.core.IO_Handler;
	import com.combiform.core.Combine_Obj;
	import com.combiform.core.IR_Event;
	import flash.display.Stage;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;
	
	public class myIO_Handler extends IO_Handler{


		public function myIO_Handler(st:Stage, uk:Boolean=true, um:Boolean=false) {
			//this code creates the arduino handler which manages the connection to all of the controllers
			super(st, uk, um);
			using_mouse=um;
			using_keyboard=uk;
		}
		
		
		
		override public function frameH():void{
			//this line needs to be added to read input from the controllers
			a_h.frameH();
		}

		override public function HandleKbdDown_Input(kevent:KeyboardEvent):void{
			trace ("myIO_H:HandleKbdDown_Input: keyCode="+kevent.keyCode)
			var co:Combine_Obj=new Combine_Obj();
			
			switch (kevent.keyCode) {
				//this is temporary fix to a known hardware issue
				//this command basically restarts the controller connection
				case 187: 
					a_h.createACs();
					trace("=");
					break; //=
				case 80: //p
					trace ("p was hit!");
					dispatchEvent(new IR_Event(IR_Event.INPUT, IR_Event.IR_OTHER, 0,0,"p"));
					break;
				case 67://c
					co.addPair([0,1]);
					co.addPair([1,2]);
					co.addPair([2,3]);
					co.addPair([3, 0]);
					
					
					
					dispatchEvent(new IR_Event(IR_Event.INPUT, IR_Event.IR_COMBINEBUTTON,0,0,"",co.clone()));
					break;
				case 87: dispatchEvent(new IR_Event(IR_Event.INPUT, IR_Event.IR_ACTIONBUTTON2,IR_Event.IR_BUTTON_DOWN,0)); break;//player 0 sends push button down W
				case 83: dispatchEvent(new IR_Event(IR_Event.INPUT, IR_Event.IR_ACTIONBUTTON2,IR_Event.IR_BUTTON_DOWN,1)); break;//player 1 sends push button down S
				case 85: dispatchEvent(new IR_Event(IR_Event.INPUT, IR_Event.IR_ACTIONBUTTON2,IR_Event.IR_BUTTON_DOWN,2)); break;//player 1 sends push button down U
				case 74: dispatchEvent(new IR_Event(IR_Event.INPUT, IR_Event.IR_ACTIONBUTTON2,IR_Event.IR_BUTTON_DOWN,3)); break;//player 1 sends push button down J
				
				default: break;
			}
		}

		override public function HandleKbdUp_Input(kevent:KeyboardEvent):void{
			switch (kevent.keyCode) {
				default: break;
			}
		}
		override public function HandleMouseClick(me:MouseEvent):void{
			if (debug) trace ( "HandleMouseClick"+me.stageX+","+me.stageY);
		}
		override public function HandleMouseDoubleClick(me:MouseEvent):void{
			if (debug) trace ( "HandleMouseDoubleClick"+me.stageX+","+me.stageY);
		}
		override public function HandleMouseMove(me:MouseEvent):void{
			if (debug) trace ( "HandleMouseDoubleClick"+me.stageX+","+me.stageY);
		}

		override public function HandleMC_Input( irevent:IR_Event):void{
			if (debug) irevent.print();
			dispatchEvent(irevent);
		}

	}
}
